home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / drawtest.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.1 KB  |  224 lines

  1. /*
  2.  * @(#)DrawTest.java    1.14 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20. import java.applet.*;
  21.  
  22. import java.util.Vector;
  23.  
  24. public class DrawTest extends Applet {
  25.     public void init() {
  26.     setLayout(new BorderLayout());
  27.     DrawPanel dp = new DrawPanel();
  28.     add("Center", dp);
  29.     add("South",new DrawControls(dp));
  30.     }
  31.  
  32.     public boolean handleEvent(Event e) {
  33.     switch (e.id) {
  34.       case Event.WINDOW_DESTROY:
  35.         System.exit(0);
  36.         return true;
  37.       default:
  38.         return false;
  39.     }
  40.     }
  41.  
  42.     public static void main(String args[]) {
  43.     Frame f = new Frame("DrawTest");
  44.     DrawTest drawTest = new DrawTest();
  45.     drawTest.init();
  46.     drawTest.start();
  47.  
  48.     f.add("Center", drawTest);
  49.     f.resize(300, 300);
  50.     f.show();
  51.     }
  52. }
  53.  
  54. class DrawPanel extends Panel {
  55.     public static final int LINES = 0;
  56.     public static final int POINTS = 1;
  57.     int       mode = LINES;
  58.     Vector lines = new Vector();
  59.     Vector colors = new Vector();
  60.     int x1,y1;
  61.     int x2,y2;
  62.     int xl, yl;
  63.  
  64.     public DrawPanel() {
  65.     setBackground(Color.white);
  66.     }
  67.  
  68.     public void setDrawMode(int mode) {
  69.     switch (mode) {
  70.       case LINES:
  71.       case POINTS:
  72.         this.mode = mode;
  73.         break;
  74.       default:
  75.         throw new IllegalArgumentException();
  76.     }
  77.     }
  78.  
  79.     public boolean handleEvent(Event e) {
  80.     switch (e.id) {
  81.       case Event.MOUSE_DOWN:
  82.         switch (mode) {
  83.           case LINES:
  84.         x1 = e.x;
  85.         y1 = e.y;
  86.         x2 = -1;
  87.         break;
  88.           case POINTS:
  89.           default:
  90.         colors.addElement(getForeground());
  91.         lines.addElement(new Rectangle(e.x, e.y, -1, -1));
  92.         x1 = e.x;
  93.         y1 = e.y;
  94.         repaint();
  95.         break;
  96.         }
  97.         return true;
  98.       case Event.MOUSE_UP:
  99.         switch (mode) {
  100.           case LINES:
  101.         colors.addElement(getForeground());
  102.         lines.addElement(new Rectangle(x1, y1, e.x, e.y));
  103.         x2 = xl = -1;
  104.         break;
  105.           case POINTS:
  106.           default:
  107.         break;
  108.         }
  109.         repaint();
  110.         return true;
  111.       case Event.MOUSE_DRAG:
  112.         switch (mode) {
  113.           case LINES:
  114.         xl = x2;
  115.         yl = y2;
  116.         x2 = e.x;
  117.         y2 = e.y;
  118.         break;
  119.           case POINTS:
  120.           default:
  121.         colors.addElement(getForeground());
  122.         lines.addElement(new Rectangle(x1, y1, e.x, e.y));
  123.         x1 = e.x;
  124.         y1 = e.y;
  125.         break;
  126.         }
  127.         repaint();
  128.         return true;
  129.       case Event.WINDOW_DESTROY:
  130.         System.exit(0);
  131.         return true;
  132.       default:
  133.         return false;
  134.     }
  135.     }
  136.  
  137.     public void paint(Graphics g) {
  138.     int np = lines.size();
  139.  
  140.     /* draw the current lines */
  141.     g.setColor(getForeground());
  142.     g.setPaintMode();
  143.     for (int i=0; i < np; i++) {
  144.         Rectangle p = (Rectangle)lines.elementAt(i);
  145.         g.setColor((Color)colors.elementAt(i));
  146.         if (p.width != -1) {
  147.         g.drawLine(p.x, p.y, p.width, p.height);
  148.         } else {
  149.         g.drawLine(p.x, p.y, p.x, p.y);
  150.         }
  151.     }
  152.     if (mode == LINES) {
  153.         g.setXORMode(getBackground());
  154.         if (xl != -1) {
  155.         /* erase the last line. */
  156.         g.drawLine(x1, y1, xl, yl);
  157.         }
  158.         g.setColor(getForeground());
  159.         g.setPaintMode();
  160.         if (x2 != -1) {
  161.         g.drawLine(x1, y1, x2, y2);
  162.         }
  163.     }
  164.     }
  165. }
  166.  
  167.  
  168. class DrawControls extends Panel {
  169.     DrawPanel target;
  170.  
  171.     public DrawControls(DrawPanel target) {
  172.     this.target = target;
  173.     setLayout(new FlowLayout());
  174.     setBackground(Color.lightGray);
  175.     target.setForeground(Color.red);
  176.     CheckboxGroup group = new CheckboxGroup();
  177.     Checkbox b;
  178.     add(b = new Checkbox(null, group, false));
  179.     b.setBackground(Color.red);
  180.     add(b = new Checkbox(null, group, false));
  181.     b.setBackground(Color.green);
  182.     add(b = new Checkbox(null, group, false));
  183.     b.setBackground(Color.blue);
  184.     add(b = new Checkbox(null, group, false));
  185.     b.setBackground(Color.pink);
  186.     add(b = new Checkbox(null, group, false));
  187.     b.setBackground(Color.orange);
  188.     add(b = new Checkbox(null, group, true));
  189.     b.setBackground(Color.black);
  190.     target.setForeground(b.getForeground());
  191.     Choice shapes = new Choice();
  192.     shapes.addItem("Lines");
  193.     shapes.addItem("Points");
  194.     shapes.setBackground(Color.lightGray);
  195.     add(shapes);
  196.     }
  197.  
  198.     public void paint(Graphics g) {
  199.     Rectangle r = bounds();
  200.  
  201.     g.setColor(Color.lightGray);
  202.     g.draw3DRect(0, 0, r.width, r.height, false);
  203.     }
  204.  
  205.     public boolean action(Event e, Object arg) {
  206.     if (e.target instanceof Checkbox) {
  207.         target.setForeground(((Component)e.target).getBackground());
  208.     } else if (e.target instanceof Choice) {
  209.         String choice = (String)arg;
  210.  
  211.         if (choice.equals("Lines")) {
  212.         target.setDrawMode(DrawPanel.LINES);
  213.         } else if (choice.equals("Points")) {
  214.         target.setDrawMode(DrawPanel.POINTS);
  215.         }
  216.     }
  217.     return true;
  218.     }
  219. }
  220.     
  221.  
  222.  
  223.     
  224.